﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpMainWindowEventHandler.h"
#include "IApplicationWindowEvent.h"
#include "IApplicationWindowEventArgs.h"
#include "IApplication.h"
#include "IMainWindow.h"
#include "GbmpUiManager.h"
#include "GbmpUiPlatformCommandIds.h"
#include "IUiDocumentViewManager.h"
#include "GmProcessServer.h"
#include "InplaceEditProjectProcessEventHandler.h"
#include "EditFamilyInProjectProcessEventHandler.h"
#include "IDocumentManager.h"
#include "LinkElementFilter.h"
#include "IStandardCodeSystemProxy.h"

#include "IGroupEvent.h"
#include "IGroupType.h"
#include "GbmpGroupEventHandler.h"

// 公共组件
#ifdef UiAdapter_IMPL
#include "GDFoundation/UiService.h"
#endif

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

CREATE_DEBUG_MODE(InstantlyPropagatesRegenData, L"即时传播RdId", gcmp::DebugModeGroup::DMGT_REGENERATION, L"GDMPLab", L"2023-12-20", L"打开此开关即时传播RdId，取代默认延时传播。");

GbmpMainWindowEventHandler::GbmpMainWindowEventHandler()
{
}

GbmpMainWindowEventHandler::~GbmpMainWindowEventHandler()
{
    IApplication* pApp = IApplication::Get();
    if (pApp)
    {
        IMainWindow* pMainWindow = IMainWindow::GetMainWindow();
        if (pMainWindow)
        {
            IApplicationWindowEvent* pApplicationWindowEvent = pMainWindow->GetMainWindowEvent();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pApplicationWindowEvent, L"pApplicationWindowEvent是空指针",L"GDMPLab",L"2024-03-30");
            pApplicationWindowEvent->Delete(this);
        }
    }
}

void GbmpMainWindowEventHandler::On(IApplicationWindowEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs是空指针",L"GDMPLab",L"2024-03-30");

    if (pArgs->GetType() == ApplicationWindowEventType::PreFirstShow)
    {

        {
            InitializeDefaultCommands();
            GbmpUiManager::GetInstance()->Init();

            //启动与族服务器通讯的server
            ProcessServer* pServer = ProcessServer::GetInstance();
            if (pServer)
            {
                pServer->RegisterProcessServerEventHandle(FunctionType::InPlaceEdit, NEW_AS_OWNER_PTR(InplaceEditProjectProcessEventHandler));
                pServer->RegisterProcessServerEventHandle(FunctionType::EditFamily, NEW_AS_OWNER_PTR(EditFamilyInProjectProcessEventHandler));
                pServer->Listen(PORT_ASSIGNED_BY_SYSTEM, 1);
            }

            if (!IDocumentManager::Get()->GetLinkDocumentElementFilter())
            {
                OwnerPtr<LinkElementFilter> opFilter = NEW_AS_OWNER_PTR(LinkElementFilter);
                IDocumentManager::Get()->SetLinkDocumentElementFilter(TransferOwnership(opFilter));
            }

            if (!IDocumentManager::Get()->GetLinkDatasetElementFilter())
            {
                OwnerPtr<ILinkDatasetElementFilter> opFilter = NEW_AS_OWNER_PTR(GdcLinkDatasetElementFilter);
                IDocumentManager::Get()->SetLinkDatasetElementFilter(TransferOwnership(opFilter));
            }

            if (DEBUG_MODE(InstantlyPropagatesRegenData))
            {
                IDocumentManager::Get()->SetInstantlyPropagatesRegenData(true);
            }

            IGroupEvent* pElementCopyEvent = IGroupType::GetGroupEvent();
            if (pElementCopyEvent)
            {
                pElementCopyEvent->Add(GbmpGroupEventHandler::Get());
            }

        }
    }
    else if (pArgs->GetType() == ApplicationWindowEventType::Prepared)
    {

    }
    else if (pArgs->GetType() == ApplicationWindowEventType::Closed)
    {
        ProcessServer* pServer = ProcessServer::GetInstance();
        if (pServer)
        {
            pServer->Stop();
        }
    }
}

void gcmp::GbmpMainWindowEventHandler::InitUiSkin(const gcmp::IApplicationWindow* pApplicationWindow)
{
#ifdef UiAdapter_IMPL
    gdmp::UiService::InitUiSkin(pApplicationWindow);
#endif
}

void GbmpMainWindowEventHandler::InitializeDefaultCommands()
{
    std::wstring modifyCommandId = ID_CMD_GBMP_MODIFY;
    IUiDocumentViewManager* pUiDocumentViewManager = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUiDocumentViewManager, L"pUiDocumentViewManager为空",L"GDMPLab",L"2024-03-30");
    pUiDocumentViewManager->SetUiViewDefaultActionCommandId(modifyCommandId);
}
